using System;
using System.Collections.Generic;
using System.Threading;
using CosyTrade.UI;
using DevExpress.XtraTreeList;
using Region=CosyTrade.Distribution.Region;
using DevExpress.XtraTreeList.Nodes;

namespace CosyTrade.ClientForm
{
    public partial class RegionDropdownControl : UserControlBase
    {
        public event EventHandler<RegionEventArgs> SelectedValue;

        public RegionDropdownControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (DesignMode) return;
            ((ThreadStart) LoadRootDataInvoke).BeginInvoke(null, null);
        }

        private void LoadRootDataInvoke()
        {
            IList<Region> rootList = Operation.ClientRelationManager.GetRegionRootList();
            Invoke((CreateTreeNodesCallback) CreateTreeNodes, null, rootList);
        }

        private delegate void CreateTreeNodesCallback(TreeListNode parentNode, IList<Region> nodeDatasource);
        private void CreateTreeNodes(TreeListNode parentNode, IList<Region> nodeDatasource)
        {
            if (parentNode == null) treeList1.Nodes.Clear();
            else parentNode.Nodes.Clear();

            foreach (Region category in nodeDatasource)
            {
                TreeListNode node = treeList1.AppendNode(new object[] { category.CaptionChina, category.CaptionSpain, category }, parentNode);
                node.HasChildren = true;
                node.Tag = false;
            }
        }

        private void treeList1_BeforeExpand(object sender, BeforeExpandEventArgs e)
        {
            if (e.Node == null || (e.Node.Tag != null && (bool)e.Node.Tag)) return;

            Region parentCategory = e.Node.GetValue("ME") as Region;
            if (parentCategory == null) return;

            e.Node.Tag = true;

            IList<Region> subList = Operation.ClientRelationManager.GetRegionSubList(parentCategory.PrimaryKey);

            CreateTreeNodes(e.Node, subList);
        }

        private void OnSelect()
        {
            if (treeList1.Selection.Count != 1) return;
            TreeListNode node = treeList1.Selection[0];
            Region category = node.GetValue("ME") as Region;
            if (category == null) return;

            if (SelectedValue != null) SelectedValue(this, new RegionEventArgs(category));
        }

        private void treeList1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks < 2) return;

            TreeListHitInfo hitInfo = treeList1.CalcHitInfo(e.Location);
            if (hitInfo.Node != null) OnSelect();
        }

        private void treeList1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            e.SuppressKeyPress = false;
            if (e.KeyCode == System.Windows.Forms.Keys.Enter) OnSelect();
        }


    }

    public class RegionEventArgs : EventArgs
    {
        private readonly Region selectCategory;

        public RegionEventArgs(Region selectCategory)
        {
            this.selectCategory = selectCategory;
        }

        public Region SelectCategory
        {
            get { return selectCategory; }
        }
    }
}
